package com.baizw.cultivation.config;

import com.baizw.cultivation.module.flowable.listener.GlobalListener;
import org.flowable.common.engine.api.delegate.event.FlowableEngineEventType;
import org.flowable.common.engine.api.delegate.event.FlowableEventDispatcher;
import org.flowable.spring.SpringProcessEngineConfiguration;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationListener;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.event.ContextRefreshedEvent;

/**
 * 全局监听配置 ContextRefreshedEvent在类被初始化之后触发
 *
 * @author 剧终1204
 * @version V1.0
 * @date 2020年10月10日 09:17:04
 */
@Configuration
public class FlowableGlobListenerConfig implements ApplicationListener<ContextRefreshedEvent> {
    
    @Autowired
    private SpringProcessEngineConfiguration configuration;
    @Autowired
    private GlobalListener globalListener;
    
    @Override
    public void onApplicationEvent(ContextRefreshedEvent event) {
        FlowableEventDispatcher dispatcher = configuration.getEventDispatcher();
        //任务创建全局监听
        dispatcher.addEventListener(globalListener,
                                    FlowableEngineEventType.TASK_CREATED,
                                    FlowableEngineEventType.TASK_ASSIGNED,
                                    FlowableEngineEventType.TASK_COMPLETED,
                                    FlowableEngineEventType.TASK_OWNER_CHANGED,

                                    FlowableEngineEventType.PROCESS_CREATED,
                                    FlowableEngineEventType.PROCESS_COMPLETED,
                                    FlowableEngineEventType.PROCESS_STARTED,

                                    FlowableEngineEventType.HISTORIC_ACTIVITY_INSTANCE_ENDED
        );
    }
}
